<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>

<body>
  <div id="app">
    <h1>{{doc.title}}</h1>
    <!-- <text-document :title="doc.title"
    v-bind:title.sync="doc.title"
    ></text-document> -->

    <text-document v-bind.sync="doc"></text-document>

    <async-example></async-example>

  </div>
  <script>
    Vue.component('text-document', {
      data() {
        return {
          newTitle: ''
        }
      },
      template: `
      <div>
          <input v-model="newTitle" />
          <button @click="handleUpdate">更新</button>
      </div>
      
      `,
      methods: {
        handleUpdate() {
          var params = {
            title: this.newTitle,
            name: 'xiaoming',
            age: 20
          }

          // this.$emit('update:title', this.newTitle)
          //这个写法是错误的
          // this.$emit('update:doc', params)
          for (let key in params) {
            this.$emit(`update:${key}`, params[key])
          }
        }
      }
    })


    Vue.component('async-example', function (resolve, reject) {
      setTimeout(function () {
        // 向 `resolve` 回调传递组件定义
        resolve({
          template: '<div>I am async!</div>'
        })
      }, 1000)
    })

    var vm = new Vue({
      el: "#app",
      data: {
        doc: {
          title: 'hello',
          name: '',
          age: 10
        }
      }
    })
  </script>
</body>

</html>